//
//  AppDelegate.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/21.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
#import "RentVieitor.h"
#import <UMSocialCore/UMSocialCore.h>
#import "MakeMoneyViewController.h"
#import "LoginViewController.h"

@interface AppDelegate ()<UIScrollViewDelegate,UITabBarControllerDelegate>

@property(nonatomic,strong)UIScrollView *scrollViewForGuidancePage;
@property(nonatomic,strong)UIPageControl *pageControlForGuidancePage;
@property(nonatomic,strong)UIButton *buttonForStart;
@end

@implementation AppDelegate  {
    
    UILabel *proLabel;
    UIView *markView;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:YES];
    
    [[UMSocialManager defaultManager] setUmSocialAppkey:UMKEY];
//    //    setPlaform是要注册的平台
//    
//    /* 微信聊天 */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxb3f466c1e03cdb1e" appSecret:@"wxb3f466c1e03cdb1e" redirectURL:@"http://mobile.umeng.com/social"];
//
//    /*QQ*/
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1106379857"  appSecret:@"nKMpsfyqPRAe99dh" redirectURL:@"http://mobile.umeng.com/social"];
//
//    /*
//     注**
//     redirectURL必须要和微博设置的回调一致
//     */
    
//    /* 新浪 */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"1029112550"  appSecret:@"c9a7aa99831c97725321fd7550adfc20" redirectURL:@"http://open.weibo.com/apps/1029112550/privilege/oauth"];
    
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyWindow];
    self.window.backgroundColor = [UIColor whiteColor];
    RootViewController *rootVc = [[RootViewController alloc] init];
    rootVc.delegate = self;
    self.window.rootViewController = rootVc;
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[Utils colorConvertFromString:@"#999999"]} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:[Utils colorConvertFromString:@"#e79327"]} forState:UIControlStateSelected];
    return YES;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    
    if (viewController.tabBarItem.tag == 2) {
        
        NSDictionary *dataDic = User_listsValue;
        
        if (dataDic == nil) {
            
            LoginViewController *loginVc = [[LoginViewController alloc] init];
            UIViewController *selectedVC = tabBarController.selectedViewController;
            [selectedVC presentViewController:loginVc animated:YES completion:nil];
            return NO;
        }

    }
    return YES;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

// 支持所有iOS系统版本回调
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
    if (!result) {
        
        [APPDELEGATE showPro:@"分享成功!"];
        // 其他如支付等SDK的回调
    }
    return result;
}

//信息提示框
- (void)showPro:(NSString *)text {
    
    [self hidePro];
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:11] constrainedToSize:CGSizeMake(300,25)];
    CGFloat width = size.width + 30;
    proLabel = [[UILabel alloc] initWithFrame:CGRectMake((KWidth-width)/2, KHeight/3*2, width, 25)];
    proLabel.text = text;
    proLabel.alpha = 0.5;
    proLabel.font = [UIFont systemFontOfSize:11];
    proLabel.backgroundColor = [Utils colorConvertFromString:@"#101010"];
    proLabel.layer.masksToBounds = YES;
    proLabel.layer.cornerRadius = 3;
    proLabel.textAlignment = NSTextAlignmentCenter;
    proLabel.textColor = [UIColor whiteColor];
    [self performSelector:@selector(hidePro) withObject:self afterDelay:2];
    [self.window addSubview:proLabel];
    
}
- (void)hidePro {
    
    [proLabel removeFromSuperview];
}

//修改成功提示框
- (void)showEditSuccess:(NSString *)text {
    
    [self hideEdit];
    markView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight)];
    markView.backgroundColor = [UIColor colorWithRed:140/255.0 green:140/255.0 blue:140/255.0 alpha:0.5];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(60, (KHeight-120)/2, KWidth-120, 120)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 2.5;
    [markView addSubview:bgView];
  
    UIImageView *logview = [[UIImageView alloc] initWithFrame:CGRectMake((bgView.frame.size.width-40)/2, 17, 40, 40)];
    logview.image = [UIImage imageNamed:@"img_complete"];
    [bgView addSubview:logview];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, KWidth-120, 30)];
    label.text = text;
    label.textColor = [Utils colorConvertFromString:@"#101010"];
    label.font = [UIFont systemFontOfSize:17];
    label.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:label];
    [self.window addSubview:markView];
    [self performSelector:@selector(hideEdit) withObject:self afterDelay:2];
}

- (void)hideEdit {
    [markView removeFromSuperview];
}

/**  核心代码 */
#pragma mark ** 判断是否第一次进入
- (void)createGuidancePage {
    
    NSLog(@"%@", NSHomeDirectory());
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *page = [defaults objectForKey:@"zhidaoye"];
    // 第一次进入， 从沙盒中读文件.
    if ([page isEqualToString:@"zhidaoye"] == NO) {
        // 沙盒中没有， 写一个文件.
        [defaults setObject:@"zhidaoye" forKey:@"zhidaoye"];
        
        NSArray *arrayOfImageView = [NSArray array];
        
        //添加引导页图片
        arrayOfImageView = @[@"引导页1", @"引导页2", @"引导页3"];
        
        self.scrollViewForGuidancePage = [[UIScrollView alloc] init];
        
        //注意:添加在window上
        [self.window addSubview:self.scrollViewForGuidancePage];
        
        self.scrollViewForGuidancePage.bounces = NO;
        
        self.scrollViewForGuidancePage.pagingEnabled = YES;
        
        self.scrollViewForGuidancePage.showsHorizontalScrollIndicator = NO;
        
        self.scrollViewForGuidancePage.userInteractionEnabled = YES;
        
        self.scrollViewForGuidancePage.frame = self.window.frame;
        
        self.scrollViewForGuidancePage.contentSize = CGSizeMake(self.window.frame.size.width * arrayOfImageView.count, 0);
        self.scrollViewForGuidancePage.backgroundColor = [UIColor redColor];
        self.scrollViewForGuidancePage.delegate = self;
        
        self.pageControlForGuidancePage = [[UIPageControl alloc] init];
        
        [self.window addSubview:self.pageControlForGuidancePage];
        
        
        
        self.pageControlForGuidancePage.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
        
        self.pageControlForGuidancePage.center = CGPointMake(self.scrollViewForGuidancePage.frame.size.width / 2, self.scrollViewForGuidancePage.frame.size.height - 40);
        
        self.pageControlForGuidancePage.numberOfPages = arrayOfImageView.count;
        
        //for循环, 在scrollView上添加照片
        for (int i = 0; i < arrayOfImageView.count; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arrayOfImageView[i]]];
            
            imageView.frame = CGRectMake(self.scrollViewForGuidancePage.frame.size.width * i, 0, self.scrollViewForGuidancePage.frame.size.width, self.scrollViewForGuidancePage.frame.size.height);
            
            [self.scrollViewForGuidancePage addSubview:imageView];
            
            imageView.userInteractionEnabled = YES;
            
            if (i == arrayOfImageView.count - 1) {
                
                //引导页最后一页"开始体验"按钮, 进入APP
                self.buttonForStart = [UIButton buttonWithType:UIButtonTypeCustom];
                
                [imageView addSubview:self.buttonForStart];
                
                [self.buttonForStart setTitle:@"开始体验" forState:UIControlStateNormal];
                
                self.buttonForStart.layer.borderWidth = 1;
                
                self.buttonForStart.layer.borderColor = [[UIColor whiteColor] CGColor];
                
                self.buttonForStart.layer.cornerRadius = 7;
                
                self.buttonForStart.layer.masksToBounds = YES;
                
                self.buttonForStart.frame = CGRectMake(0, 0, self.window.frame.size.width / 2, 40);
                
                self.buttonForStart.center = CGPointMake(self.window.frame.size.width / 2, self.window.frame.size.height - 100);
                
                [self.buttonForStart addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];
                
            }
            
        }
        
    }
    
}

- (void)start {
    
    //从父视图上移除
    [self.pageControlForGuidancePage removeFromSuperview];
    
    [self.scrollViewForGuidancePage removeFromSuperview];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.pageControlForGuidancePage.currentPage = self.scrollViewForGuidancePage.contentOffset.x / self.window.frame.size.width;
    
}




@end
